<?php 
/**
 * 会员Action
 * @author tangbl
 *
 */
class MemberAction extends CommonAction{
	
	
	/**
	 * 会员中心首页
	 * 显示个人资料
	 */
	public function index(){
		
		if(IS_AJAX){
			
			$json = sendRest("MMS/Member/edit",$_POST);
			$this->ajaxReturn($json);
		}else{
			
			$map['id'] = cookie(C('USER_AUTH_KEY'));
			$json      = sendRest("MMS/Member/detail",$map);
			$user      = $json['data'];
			
			$address = D("Address")->getAddressDict();
			$user['dict'] = $address;
			$this->assign("user",$user);
			$this->display();
		}
	}
	
	/**
	 * 校验数据的唯一性
	 */
	public function changePassword(){
	
		if(IS_AJAX){
			
			$map['id']       = cookie(C('USER_AUTH_KEY'));
			$map['password'] = $this->_param("oldpassword","trim","");
	
			$json = sendRest("MMS/Member/checkPassword",   $map);
			$this->ajaxReturn($json);
		}else{
			$this->display();
		}
		
		
	}
	/**
	 * 密码修改
	 */
	public function changePass(){
	
		if(IS_AJAX){
				
			$_POST['id'] = cookie(C('USER_AUTH_KEY'));
			$res = sendRest("MMS/Member/editPassword",$_POST);
				
			if($res['status']==1){
				$member = D("Member");
				$member->clearLoginSession();
			}
			$this->ajaxReturn($res);
		}
	}
	
	/**
	 * 会员订单列表
	 */
	public function order_index(){
		
		//会员订单列表
 		$map['dataid'] = cookie(C('USER_AUTH_KEY'));
 		$json = sendRest("OMS/Order/order_select",$map);
 		$list = $json['data'];
 		
 		$model = M('Outlet');
 		foreach($list as $k=>$v){
 			$detail = $model->find($v['outletid']);
 			$list[$k]['outlet']   = $detail['title'];
 			$list[$k]['enoutlet'] = $detail['entitle'];
 		}
 		$this->assign("orderlist",$list);
 		
 		//订单状态
 		$status   = C('ORDER_STATUS');
 		$enstatus = C('ORDER_ENSTATUS');
 		
 		$this->assign('status',$status);
 		$this->assign('enstatus',$enstatus);
		$this->display();
	}
	
	/**
	 * 会员订单详情
	 */
	public function order_detail(){
		
		$orderno = $this->_get("orderno","trim","");
		$map['orderno'] = $orderno;
		$json  = sendRest("OMS/Order/byorderno",$map);
		
		$order = sendRest("OMS/Order/orderbyno",$map);
		$this->assign("order",$order['data']);
		
		$data = $json['data'];
		foreach ($data as $k=>$v){
			if($v['flag']=="dishes"){
				$dishes[$k] = $v;
				if($v['attr']){
					$attr = json_decode($v['attr'],true);
					$dishes[$k]['attr'] = $attr;
				}
				
			}else{
				$drink[$k]  = $v;
			}
		}
		$this->assign("dishes",$dishes);
		$this->assign("drink", $drink);
		
		$this->display();
	}
	
	/**
	 * 确认收货 
	 */
	public function order_confirm(){
		
		$map['userid'] = cookie(C('USER_AUTH_KEY'));
		$map['id']     = $this->_param("id","intval",0);
		$map['status'] = $this->_param("act","intval",5);
		$json = sendRest("Order/userStatus",$map);
		
		if($map['status'] == 5){
			call_user_func("sence_check",'qianshou_ok','ACTIVE',1,$this);
			call_user_func("sence_check",'qianshou_ok','YHJ',1,$this);
		}
		$this->ajaxReturn($json);
	}
	
	/**
	 * 删除订单
	 */
	public function order_close(){
		
		$map['userid'] = cookie(C('USER_AUTH_KEY'));
		$map['id']     = $this->_param("id","intval",0);
		$map['status'] = -1;
		$json = sendRest("Order/userStatus",$map);
		
		$this->success($json['info'],U("Member/order_index"));
	}
	
	/**
	 * 会员订单评论
	 */
	public function order_comment(){
		
		$id = $this->_param("id","intval",0);
	
		if(IS_POST){
			
			$title = $_POST['title'];
			$score = $_POST['score'];
			$model = D("Order");
			$flag  = $model->orderComment($title,$score,$id);
			
			if($flag){
				$this->success(L('mb_form_recomment_success'),U('Member/order_index'));
			}else{
				$this->error(L('mb_form_recomment_failurl'));
			}
			
		}else{
			
			$map['userid'] = session(C('USER_AUTH_KEY'));
			$map['id']     = $id;
			$map['detail'] = 1;
			$json = sendRest("Order/userDetail",$map);
			
			if($json['status']!=1){
				$this->error("非常抱歉，该订单暂时不能评论~！");
			}
			
			$this->assign("detail",$json['data']);

			$this->display();
		}
	}

	/**
	 * 复制订单
	 */
	public function addOrder(){
		
		$id      = $this->_param('id','intval',0);
		$model   = M('OrderDetail');
		$user    = cookie(C('USER_AUTH_KEY'));
		$orderno = createOrderNo();
		
		if($id && $user){
			
			//查询订单
			$map['id'] = $id;
			$map['table'] = 'Order';
			$data = sendRest('BDC/Content/contentDetail',$map,'post');
			$data = $data['data'];
			
			if($data['id']){
				
				//查询订单详情
				$where['orderno'] = $data['orderno'];
				$list = $model->where($where)->select();
				
				//添加订单详情
				foreach($list as $k=>$v){
					unset($v['id']);
					$v['orderno'] = $orderno;
					$v['addtime'] = time();
					$model->add($v);
				}
				
				//添加订单
				unset($data['id']);
				unset($data['time_que']);
				unset($data['time_dao']);
				unset($data['time_pei']);
				unset($data['time_shou']);
				unset($data['dealid']);
				$data['status']    = 1;
				$data['edltime']   = time()+$data['edltime']-$data['sdltime'];
				$data['sdltime']   = time();
				$data['orderno']   = $orderno;
				$data['addtime']   = time();
				$data['ordertime'] = time();
				$data['isyuding']  = 2;
				
				if(M('Order')->add($data)){
					$this->ajaxReturn(array('status'=>1));
				}else{
					$this->ajaxReturn(array('status'=>-1,'info'=>L('mb_eme_ele_orderfaile')));
				}
			}else{
				$this->ajaxReturn(array('status'=>-1,'info'=>L('mb_eme_ele_ordernone')));
			}
		}else{
			$this->ajaxReturn(array('status'=>-1,'info'=>L('mb_eme_ele_orderselect')));
		}
	}
	
	/**
	 * 会员地址列表和会员地址删除
	 */
	public function address_index(){

		$member = D('Member');
		$cityid = $member->getUserCityid();
		
		if(IS_AJAX){
			$map['keywords'] = $this->_param('value','trim','');
			$map['cityid']   = $this->_param('cityid','intval',0);
			$map['cityid']   = $map['cityid']==""?$cityid:$map['cityid'];
			$json = sendRest("RMS/Site/select",$map,'post');
				
			$html = "";
			foreach($json['data'] as $k=>$v){
				
				$lang    = getLang();
				$address = $lang=='zh-cn'?$v['address']:$v['enaddress'];
				$html .= "<dd><span cityid='".$v['city']."' longitude='".$v['longitude']."' latitude='".$v['latitude']."' did='".$v['id']."'>".$address."</span></dd>";
			}
			
			$this->ajaxReturn($html);
		}else{
		
			$userid = cookie(C('USER_AUTH_KEY'));
			$map    = array("userid"=>$userid);
			$json   = sendRest("RMS/Address/byUser",$map);
			$this->assign("list",$json['data']);
			
			$this->display();
		}
	}
	
	public function address_add(){
		
		$userid = cookie(C('USER_AUTH_KEY'));
		
		if(empty($_POST['longitude'])||empty($_POST['latitude'])){
			$this->error(L('mb_mem_ele_addserror'));exit;
		}
		
		if(empty($_POST['city'])){
			$this->error(L('mb_mem_ele_selectcity'));exit;
		}
		
		$_POST['userid']  = $userid;
		$_POST['address'] = $_POST['ads'].$_POST['detail'];
		$json = sendRest("RMS/Address/front_add",$_POST);
		
		if($json['status']==1){
			$this->success(L('mb_mem_ele_addads'),U('Member/address_index'));
		}else{
			$this->success(data.info);
		}
	}
	
	public function addrdel(){
		
		if(IS_AJAX){
			$map['id'] = $this->_param("id","intval",0);
			$json = sendRest("RMS/Address/del",$map);
			$this->ajaxReturn($json);
		}
	}
	
	/**
	 * 修改会员地址
	 */
	public function address_edit(){

		if(IS_POST){
			
			$_POST['address'] = $_POST['ads'].$_POST['detail'];
			$json = sendRest("RMS/Address/front_edit",$_POST);
			if($json['status']==1){
				$this->success(L('mb_mem_ele_editads'),U('Member/address_index'));
			}else{
				$this->success(data.info);
			}
		}else{
			
			$id     = $this->_param("id","intval",0);
			$userid = cookie(C('USER_AUTH_KEY'));
			$map    = array("userid"=>$userid,"id"=>$id);
			$json   = sendRest("RMS/Address/detail",$map);
			$this->assign("detail",$json['data']);
			
			$data = sendRest("BDC/Outlet/dictAddress");
			$this->assign("dictAddressCN",$data['data']['zh-cn']['array']);
			$this->assign("dictAddressEN",$data['data']['en-us']['array']);
			
			$this->display();
		}
		
		
	}
	/**
	 * 显示VIP会员卡详情
	 * $json json对象   获取用户vip卡信息
	 */
	public function vip_index() {
		
		$model   = D("Actvipcard");
		$userid  = session(C('USER_AUTH_KEY'));
		$json    = $model->getUserVipDetail($userid);
		
		if($json['status'] == 1){
			$this->assign("detail",$json);
			$this->display();
		}else{
			$this->error("该用户暂无会员卡,请先领取",U("Member/index"));	
		}
	}
	
	/**
	 * VIP会员卡积分操作记录
	 * $userid  int  会员编号
	 */
	public function vip_credit() {
		
		$model  = D("Actvipcard");
		$userid = session(C('USER_AUTH_KEY'));
	 
		$map['p']      = $this->_param("p","intval",1);
		$map['userid'] = $userid;
		$map['pagenum']= 5;
		$map['page']   = "index.php";
		$map['url']    = "Member/vip_credit";
		
		$json = $model->getUserVipCredit($map);
		$this->assign("json",$json);
		
		$this->display();
	}
	
	/**
	 * VIP会员卡金额操作记录
	 * $userid int 会员编号
	 */

	public function vip_money() {
		
		$model  = D("Actvipcard");
		$userid = session(C('USER_AUTH_KEY'));
		
		$map['p']      = $this->_param("p","intval",1);
		$map['userid'] = $userid;
		$map['pagenum']= 5;
		$map['page']   = "index.php";
		$map['url']    = "Member/vip_money";
		
		$json = $model->getUserVipMoney($map);
		$this->assign("json",$json);

		$this->display();
	}
	
	/**
	 * 完善会员卡资料
	 * $userid int 会员编号
	 * 读取vipcard_user的数据
	 */
	public function vip_perfect() {
		$member = M("Member");
		$userid = session(C('USER_AUTH_KEY'));
		if(IS_POST){
			$validate = array(
				array('realname','','{%mb_form_validata_realname_required}',1,''),
				array('address','','{%mb_form_validata_address_required}',1,''),
			);
			
			if($member->create()){
				$result = $member->where("id=$userid")->save();
				
				$this->assign("jumpUrl",U('Member/vip_index')); 
				$this->success(L('cfg_form_tipmsg_editsucc'));	
			}else{
				$this->error($member->getError());	
			}
		}else{
			$data = $member->where("id=$userid")->find();
			
			$this->assign("detail",$data);
			$this->display();
		}
	}
	
	/**
	 * 会员卡说明
	 * $userid int 会员编号
	 * list array 返回结果集
	 */
	public function vip_instruction() {
		$model  = D("Actvipcard");
		$userid = session(C('USER_AUTH_KEY'));
		
		$list = $model ->getVipInstruction($userid);
		$this->assign("list",$list);
		
		$this->display();
	}
	
	/**
	 * 通用名店地址
	 * $outlet array 获取缓存结果集
	 */
	public function vip_outlet() {
		$outlet = F("MAP_POI");
		
		$this->assign("outlet",$outlet);
		$this->display();
	}

	/**
	 * 绑定实体会员卡
	 * 
	 */
	public function vip_bangding(){
		if(IS_POST){
			$this->assign("jumpUrl",U('/Member/index'));
			$this->success("恭喜您，绑定实体会员卡成功");
		}else{
			$this->display();
		}
	}
	
	/**
	 * 用户优惠卷列表
	 * 删除优惠卷
	 */
	public function coupon_index() {
		
		$model  = D("Actyhj");
		$userid = session(C('USER_AUTH_KEY'));
		$map['userid'] = $userid;
		
		if(IS_AJAX){
			
			$map['couponid'] = $this->_param('id');
			$json = sendRest("Coupon/del",$map);
			
			$this->ajaxReturn($json['info']);	
		}else{

			$map['format'] = 1;
			$list = $model->getUserCoupon($map);
			$this->assign("list",$list);
					
			$this->assign("empty",$this->fetch("Empty:coupon_index"));
			
			$this->display();
		}
	}
	
	/**
	 * 优惠卷详情
	 * 
	 */
	public function coupon_detail() {
		
		$model  = D("Actyhj");
		$id     = $this->_param("id");
		$userid = session(C('USER_AUTH_KEY')); 
		
		$json = $model->getUserCouponDetail($id,$userid);
	
		$this->assign("detail",$json);	
		$this->display();
	}
	
	/**
	 * 显示个人资料-更多信息
	 */
	public function member_info(){
		
		$member = D("Member");
		$userid = cookie(C('USER_AUTH_KEY'));
		
		if(IS_POST){
			
			$model = M("MemberInfo");
			
			if($model->create()){
				
				$model->where("userid=".$userid)->save();
				$flag = $member->setMemberSession($userid);
				$this->success(L('mb_mem_ele_edituser'),U('Member/index'));
			}else{
				$this->error($member->getError());
			}
				
		}else{

			$model  = D("MemberInfoView");
			$user   = $model->find($userid);
			$this->assign("user",$user);
		
			$this->display();
		}
	}
	
	/**
	 * 修改和显示个人资料
	 */
	public function member_edit(){
	
		$member = D("Member");
		$userid = cookie(C('USER_AUTH_KEY'));
		
		if(IS_POST){
			
			$model = M("MemberInfo");
			if($model->create()){
				$id  = M("MemberInfo")->where("userid=".$userid)->getField("id");
				if($id){
					$model->id = $id;
					$model->save($data);
				}else{
					$model->userid   = $userid;
					$model->username = session(C('USER_AUTH_ACCOUNT'));
					$model->solevar  = create_id();
					$model->addtime  = time();
					$model->where("userid=".$userid)->add();
				}
				$flag = $member->setMemberSession($userid);
				$this->success(L('mb_mem_ele_edituser'),U('Member/index'));
			}else{
				$this->error($member->getError());
			}
				
		}else{
		
			$model  = D("MemberInfoView");
			$user   = $model->find($userid);
			$address= D("Address")->getAddressDict();
			$user['dict'] = $address;
			$this->assign("user",$user);
			$this->display();
		}
	}
	
	/**
	 * 会员收藏夹列表
	 */
	public function member_collect(){
	
		$model   = D("MemberCollect");
		$userid  = cookie(C('USER_AUTH_KEY'));
	
		if(IS_AJAX){
			$id      = $this->_param("id","intval","0");
				
			$map     = array("userid"=>$userid,"id"=>$id);
			$res     = $model->delUserCollect($map);
				
			$this->success(L('cfg_form_tipmsg_delsucc'));
		}else{
			$data    = $model->getUserCollect($userid);
				
			$this->assign("empty",$this->fetch("Empty:member_collect"));
			$this->assign("list",$data['data']);
			$this->assign("show",$data['show']);
			$this->display();
		}
	}
	
	/**
	 * 会员修改密码
	 */
	public function member_password(){
	
		if(IS_POST){
			
			$model = D("Member");
			$_password  = array(
				array('oldpassword','/^.{6,16}$/','{%mb_model_ele_password2}',1,'regex'),
				array('newpassword','/^.{6,16}$/','{%mb_model_ele_password3}',1,'regex'),
				array('repassword','newpassword','{%mb_model_ele_password4}',0,'confirm',3),
			);	
			$model->setProperty("_validate",$_password);
				
			if($model->create()){
				$userid = session(C('USER_AUTH_KEY'));
				$flag   = $model->editUserPassword($userid);
	
				if($flag){
					$this->success(L('mb_form_validata_password_tip_suc'),U('Member/index'));
				}else{
					$this->error(L('mb_form_validata_password_tip'));
				}
			}else{
				$this->error($model->getError());
			}
		}else{
			$this->display();
		}
	}
	
	/**
	 * 会员积分兑换记录
	 * $userid int 会员编号
	 */
	public function member_integral(){
		$model  = D("Integral");
		$userid = session(C('USER_AUTH_KEY'));
		
		$data = $model->getUserIntegralList($userid);

		$this->assign("empty",$this->fetch("Empty:member_integral"));
		$this->assign("list",$data);
		$this->display();
	}
	
	/**
	 * 会员数据统计
	 * 暂时未使用
	 */
	public function member_total(){
	
		$model = D("Total");
		$total = $model->memberTotal();
		$this->assign("total",$total);
	
		$this->display();
	}
}
?>